<?php
/*----------------------------------------------------------------------
 * 后台基础控制
 * Author: ming.nie
----------------------------------------------------------------------*/

namespace Admin\Controller;
use Think\Controller;

class AdminController extends Controller {

    protected function _initialize(){
        C('LAYOUT_NAME', 'Public/layout');
        C('PAGE_TITLE', '影院运营管理平台');
        
        $userLogic = new \Common\Logic\UserLogic();
        $this->user_info = $userLogic->getCurrentUserInfo();
        $user_info = $this->user_info;
        if(!$this->user_info) {
            header('Location: '.U('Login/index'));
            exit;
        }
        define('UID', $this->user_info['user_id']);
        
        //验证权限
        if('admin' == $this->user_info['user_name']) {
            $check_rull = true;
        } else {
            $check_rull = $this->checkRule();
        }
        
        if(!$check_rull) {
            $this->error('对不起，您没有该权限~', U('Index/index'), 3);
        }
        
        //设置菜单选中
        $musc = I('request.musc', 0);
        if(0 < $musc) {
            cookie('musc', $musc, 999999);
        }
        $musc = cookie('musc');
        //echo $musc.'a';
        
        //设置主菜单选中
        $mmusc = I('request.mmusc', 0);
        if(0 < $mmusc) {
            cookie('mmusc', $mmusc, 999999);
        }
        $mmusc = cookie('mmusc');
        //echo $mmusc.'b';

        //获取子菜单数组
        foreach($this->user_info['menu_list_level'] as $key => $value) {
            if($mmusc == $value['menu_id']) {
                $sub_menu_list = $value['sub_list'];
            }
        }
        $this->assign(get_defined_vars());
    }

    //验证用户权限
    final protected function checkRule(){
        $result = false;
        //全局授权操作判断
        $global_permission = getGlobalPermissions();
        if(in_array(__ACTION__, $global_permission)) {
            $result = true;
        } else {
            //获取用户菜单权限数组
            $current_url = strtolower(__ACTION__);
            //获取用户菜单权限数组
            foreach($this->user_info['menu_list'] as $key => $value) {
                $value['url'] = strtolower($value['url']);
                $check_url = $value['url'];
                if('#' != $value['url']) {
                    $check_url = U($value['url']);
                }
                if(is_numeric(strpos($current_url, $check_url))) {
                    $result = true;
                    break;
                }
            }
        }
        
        return $result;
    }
}
